package com.ctms.system.controller;

import com.ctms.common.utils.PageUtils;
import com.ctms.common.utils.Query;
import com.ctms.common.utils.R;
import com.ctms.common.utils.ShiroUtils;
import com.ctms.system.domain.CourseDO;
import com.ctms.system.domain.CourseStudentDO;
import com.ctms.system.domain.UserDO;
import com.ctms.system.service.CourseService;
import com.ctms.system.service.CourseStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/myStudent")
@Controller
public class MyStuCourseController {

    @Autowired
    CourseService courseService;
    @Autowired
    CourseStudentService courseStudentService;

    @GetMapping("")
    public String myStudent(){
        return "sys/myCourse/myStuCourse";
    }

    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String, Object> params) {
        UserDO userDO = ShiroUtils.getUser();
        int loginId = Integer.parseInt(Long.toString(userDO.getUserId()));
        params.put("loginId",loginId);
        // 查询列表数据
        Query query = new Query(params);
        List<CourseDO> courseDOS =courseStudentService.selectStuCourse(params);
        for (int i=0;i<courseDOS.size();i++){
            String teacherId=courseDOS.get(i).getCourseTeacher();
            String[] split=teacherId.split(",");
            String teacher="";
            for (int j=0;j<split.length;j++){
                String teacherName = courseService.getNameById(Integer.parseInt(split[j]));
                if (j != split.length-1){
                    teacher = teacher + teacherName + ",";
                }else {
                    teacher = teacher + teacherName;
                }
            }
            System.out.println(teacher);
            courseDOS.get(i).setCourseTeacher(teacher);
        }
        int total= courseStudentService.count(query);
        PageUtils pageUtil = new PageUtils(courseDOS, total);
        return pageUtil;
    }

    @PostMapping("/remove")
    @ResponseBody
    R deleteStuCourse(Integer cid){
        if (courseStudentService.deleteStuCourse(cid)>0){
            return R.ok();
        }else {
            return R.error();
        }
    }
}
